package com.jinyu.jyly.ui.mine.make_code.change;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.utils.AES;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 修改银行账户
 */
public class ChangeBankCodeActivity extends BaseActivity<ChangePaymentPresenter> implements ChangePaymentContract.View {


    @BindView(R.id.fake_status_bar)
    View fakeStatusBar;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_title_view)
    RelativeLayout rlTitleView;
    @BindView(R.id.tv_bank_name)
    EditText tvBankName;
    @BindView(R.id.tv_bank_num)
    EditText tvBankNum;
    @BindView(R.id.tv_user_name)
    EditText tvUserName;
    @BindView(R.id.tv_bank_zhi_name)
    EditText tvBankZhiName;
    @BindView(R.id.tv_psw)
    EditText tvPsw;
    @BindView(R.id.btn_sub)
    Button btnSub;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    private ChangePaymentPresenter presenter;
//    private String deviceId;

    @Override
    public int getLayout() {
        return R.layout.activity_change_bank;
    }

    @Override
    protected ChangePaymentPresenter getPresenter() {
        presenter = new ChangePaymentPresenter(this, this);
        return presenter;
    }

    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarDarkMode(this);
//        deviceId = GetDeviceId.getDeviceId(this);
    }


    @Override
    public void onFailer(Throwable throwable) {

    }

    @Override
    public void onPayMent(AllInfoBean infoBean) {
        switch (infoBean.getStatus()) {
            case 200:
                String data = infoBean.getData();
                showToast("修改成功");
                finish();
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:break;
        }
    }

    @Override
    public void onUpdataImg(AllInfoBean infoBean) {


    }

    private int mSmsId;

    @Override
    public void onSmsCode(AllInfoBean infoBean) {
        mTipDialog.dismiss();
        switch (infoBean.getStatus()) {
            case 200:
                try {
                    String data = infoBean.getData();
                    String result = AES.decrypt(data);
                    JSONObject jsonObject = new JSONObject(result);
                    mSmsId = jsonObject.optInt("id");
                    downTimer.start();
                    tvGetCode.setEnabled(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                tvGetCode.setEnabled(true);
                tvGetCode.setText("获取验证码");
                showToast(infoBean.getMsg());
                break;
            default:
                break;
        }
    }


    CountDownTimer downTimer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long arg0) {
            tvGetCode.setText("重新发送(" + arg0 / 1000 + ")");
            tvGetCode.setEnabled(false);
        }

        @Override
        public void onFinish() {
            tvGetCode.setEnabled(true);
            tvGetCode.setText("获取验证码");
        }
    };


    @OnClick({R.id.iv_back, R.id.btn_sub, R.id.tv_get_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code:
                //获取验证码
                boolean showing = mTipDialog.isShowing();
                if (!showing) {
                    mTipDialog.show();
                    presenter.getSmsCode();
                }
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_sub:
                String bankName = tvBankName.getText().toString();
                String bankNum = tvBankNum.getText().toString();
                String userName = tvUserName.getText().toString();
                String banzhiName = tvBankZhiName.getText().toString();
                String psw = tvPsw.getText().toString();
                String smscode = et_code.getText().toString().trim();
                if(TextUtils.isEmpty(bankName)){
                    showToast("开户银行不能为空");
                    return;
                }
                 if(TextUtils.isEmpty(bankNum)){
                    showToast("银行卡号不能为空");
                    return;
                }
                 if(TextUtils.isEmpty(userName)){
                    showToast("收款人不能为空");
                    return;
                }
                 if(TextUtils.isEmpty(banzhiName)){
                    showToast("开户支行银行不能为空");
                    return;
                }
                if(TextUtils.isEmpty(psw)){
                    showToast("交易密码不能为空");
                    return;
                }
                if (TextUtils.isEmpty(smscode)) {
                    showToast("验证码不能为空");
                    return;
                }

                try {
                    //修改
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("bank", bankName); //37
                    jsonObject.put("bank_num", bankNum); //37
                    jsonObject.put("bank_name", userName); //37
                    jsonObject.put("bank_little_name", banzhiName); //37
                    jsonObject.put("password", psw); //37
                    jsonObject.put("cat", "bank"); //37
//                    jsonObject.put("code", deviceId); //37
                    jsonObject.put("smsid", mSmsId); //37
                    jsonObject.put("verycode", smscode); //37
                    String param = AES.encryptToHexString(jsonObject.toString());
                    presenter.setPayMent(param);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
